/*
 * Giffel, Android application
 *
 * Copyright (C) 2011  Mid Sweden University
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package se.appening;

import se.appening.SubjectEvent.EventArg;
import se.appening.SubjectEvent.Listener;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

public class MainController implements Listener {

    private Context context;
    private GeoLocHandler geoLocHandler;
    private CringleGetter cringleGetter;

    public MainController(Context context) {
        this.context = context;
        this.cringleGetter = new LiveCringleGetter();
        initialize();
    }

    public MainController(Context context, CringleGetter cringleGetter) {
        this.context = context;
        this.cringleGetter = cringleGetter;
    }

    private void initialize() {
        LocationManager locationService =
            (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        geoLocHandler = new GeoLocHandler(locationService);
        geoLocHandler.getLocationChangedEvent().add(this);
    }

    public void invoke(String subjectTag, EventArg event) {
        if (subjectTag.contentEquals("GeoLoc")) {
            LocationChanged loc = (LocationChanged) event;
            try {
                cringleGetter.updatePosition(loc.position);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    void setNrOfEntries(int entries) {
        cringleGetter.setNrCringles(entries);
    }

    int getNrOfEntries() {
        return cringleGetter.getNrCringles();
    }

    public Entry[] getEntries() {
        return cringleGetter.getEntries();
    }

    public Location getCurrentLocation() {
        return geoLocHandler.getCurrentLocation();
    }

}
